<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ include file="../../../../header.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
#manage-form {
	display: table;
	margin:0 auto;
}

.table-row {
	display: table-row;
	height:32px;
}
.ui-input{
	width: 200px;
}
.table-row>* {
	display: table-cell;
	margin-left: 20px;
	vertical-align: middle;
}
.table-row>label{
	text-align: right;
}

</style>
</head>
<body style="align: center;">
	<div style="margin-top: 10px; margin-left: 30px;">
		<form id="manage-form" action="">
			<div class="table-row">
				<!--'操作','部门编码', '部门名称', '所属公司', '上级部门', '编码级别','末级部门','财务','销售','采购','设备','负责人','电话','传真','启用'-->
				<label for="jobcode">部门编码:</label> <input type="text"
					class="ui-input" name="tradename" id="deptCode">
			</div>
			<div class="table-row">
				<label for="jobcode">部门名称:</label> <input type="text"
					class="ui-input" name="tradename" id="deptName">
			</div>
			<div class="table-row">
				<label for="jobcode">所属公司:</label> <select id="companyId" 
					class="ui-input">
					<c:forEach var="company" items="${ companys}">
						<option value="${company.id}">${company.companyname }</option>
					</c:forEach>
				</select>
			</div>
			<div class="table-row">
				<label for="jobcode">上级部门:</label> <select id="parentId"
					class="ui-input">
					<c:forEach var="dept" items="${ departments}">
						<option value="${dept.id}">${dept.deptName }</option>
					</c:forEach>

				</select>
			</div>
			<div class="table-row">
				<label for="jobcode">编码级别:</label> <input type="text"
					class="ui-input" name="tradename" id="codeLevel">
			</div>
			<div class="table-row">
				<label for="jobcode">末级部门:</label>
				<div id="endFlag">
					<input  class="switch-simple-3" id="switch_endFlag" type="checkbox" ><label for="switch_endFlag" class="green"></label>
				</div>
			</div>
			<div class="table-row">
				<label for="jobcode">财务:</label>
				<div id="cwzzFlag">
						<input  class="switch-simple-3" id="switch_cwzzFlag" type="checkbox" ><label for="switch_cwzzFlag" class="green"></label>
				</div>
			</div>
			<div class="table-row">
				<label for="jobcode">销售:</label>
				<div id="xsFlag">
					<input  class="switch-simple-3" id="switch_xsFlag" type="checkbox" ><label for="switch_xsFlag" class="green"></label>
				</div>
			</div>
			<div class="table-row">
				<label for="jobcode">采购:</label>
				<div id="cgFlag">
					<input  class="switch-simple-3" id="switch_cgFlag" type="checkbox" ><label for="switch_cgFlag" class="green"></label>
				</div>
			</div>
			<div class="table-row">
				<label for="jobcode">设备:</label>
				<div id="devFlag">
					<input  class="switch-simple-3" id="switch_devFlag" type="checkbox" ><label for="switch_devFlag" class="green"></label>
				</div>
			</div>
			<div class="table-row">
				<label for="jobcode">负责人:</label> <input type="text"
					class="ui-input" name="tradename" id="principal">
			</div>
			<div class="table-row">
				<label for="jobcode">电话:</label> <input type="text" class="ui-input"
					name="tradename" id="telephone">
			</div>
			<div class="table-row">
				<label for="jobcode">传真:</label> <input type="text" class="ui-input"
					name="tradename" id="fax">
			</div>
			<div class="table-row">
				<label for="jobcode">启用:</label>
				<div id="active">
					<input  class="switch-simple-3" id="switch_active" type="checkbox" ><label for="switch_active" class="green"></label>
				</div>
			</div>

		</form>
	</div>

	<script type="text/javascript">
		var api = frameElement.api;
		var operate = api.data.operate;
		var rowid = api.data.rowid;
		var rowdata = api.data.rowdata;
		var CallBack = api.data.callback;
		$(function() {
			initPopBtns();
			if (operate == "edit") {
				$("#deptCode").val(rowdata.deptCode);
				$("#deptName").val(rowdata.deptName);
				console.log(rowdata.companyId);
				$("#companyId option").filter(function() {
					return $(this).text() == rowdata.companyId;
				}).prop("selected", true);
				$("#parentId option").filter(function() {
					return $(this).text() == rowdata.parentId;
				}).prop("selected", true);
				$("#codeLevel").val(rowdata.codeLevel);
				$("#endFlag").html(rowdata.endFlag);
				$("#cwzzFlag").html(rowdata.cwzzFlag);
				$("#xsFlag").html(rowdata.xsFlag);
				$("#cgFlag").html(rowdata.cgFlag);
				$("#devFlag").html(rowdata.devFlag);
				$("#principal").val(rowdata.principal);
				$("#telephone").val(rowdata.telephone);
				$("#fax").val(rowdata.fax);
				$("#active").html(rowdata.active);

			}
			$('input[type=checkbox]').attr("disabled",false);
		})
		function initPopBtns() {
			var btnname = "add" == operate ? [ "保存", "关闭" ] : [ "确定", "取消" ];
			api.button({
				id : "confirm",
				name : btnname[0],
				focus : true,
				callback : function(e) {
					var deptCode = $("#deptCode").val();
					var deptName = $("#deptName").val();
					var companyId = $("#companyId").val();
					var parentId = $("#parentId").val();
					var codeLevel = $("#codeLevel").val();
					var endFlag = $("#endFlag input").prop("checked");
					var cwzzFlag = $("#cwzzFlag input").prop("checked");
					var xsFlag = $("#xsFlag input").prop("checked");
					var cgFlag = $("#cgFlag input").prop("checked");
					var devFlag = $("#devFlag input").prop("checked");
					var principal = $("#principal").val();
					var telephone = $("#telephone").val();
					var fax = $("#fax").val();
					var active = $("#active input").prop("checked");
					if (operate == "add") {
						$.ajax({
							url : '/dept/gridCUD?oper=add',
							type : 'POST',
							dataType : "json",
							data : {
								"deptCode" : deptCode,
								"deptName" : deptName,
								"companyId" : companyId,
								"parentId" : parentId,
								"codeLevel" : codeLevel,
								"endFlag" : endFlag,
								"cwzzFlag" : cwzzFlag,
								"xsFlag" : xsFlag,
								"cgFlag" : cgFlag,
								"devFlag" : devFlag,
								"principal" : principal,
								"telephone" : telephone,
								"fax" : fax,
								"active" : active,
							},
							async : false,
							success : function(data) {
								if (data.status == 200) {
									parent.Public.tips({
										content : "添加成功"
									})
									CallBack();
								} else {
									parent.Public.tips({
										type : 1,
										content : "添加失败:" + data.message
									})
								}
							}
						});
					} else {
						$.ajax({
							url : encodeURI("/dept/gridCUD?oper=edit"),
							type : "POST",
							data : {
								"id" : rowid,
								"deptCode" : deptCode,
								"deptName" : deptName,
								"companyId" : companyId,
								"parentId" : parentId,
								"codeLevel" : codeLevel,
								"endFlag" : endFlag,
								"cwzzFlag" : cwzzFlag,
								"xsFlag" : xsFlag,
								"cgFlag" : cgFlag,
								"devFlag" : devFlag,
								"principal" : principal,
								"telephone" : telephone,
								"fax" : fax,
								"active" : active,
							},
							async : false,//同步,坑爹啊。。
							success : function(data) {
								if (data.status == 200) {
									parent.Public.tips({
										content : "修改成功"
									})
									CallBack();
								} else {
									parent.Public.tips({
										type : 1,
										content : "修改失败:" + data.message
									})
								}
							}
						});
					}
				}
			}, {
				id : "cancel",
				name : btnname[1]
			})
		}
	</script>
</body>
</html>